package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartFeedbackResponse;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceFeedbackConfirmFinishFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceFeedbackWaitConfirmFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceFeedbackWaitReplyFragment;
import com.cloudsigns.factorymanager2.produce.iview.IProductFeedbackListView1;
import com.cloudsigns.factorymanager2.produce.presenter.ProductFeedbackListPresenter1;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ProduceFeedbackListFragmentActivity extends FragmentActivity implements View.OnClickListener, IProductFeedbackListView1 {
    private final String TAG = ProduceFeedbackListFragmentActivity.class.getSimpleName();
    private RelativeLayout rl_back,rl_right;
    private TextView tv_title;
    private View ll_wait_confirm, ll_wait_reply, ll_confirm_finish;
    private TextView tv_wait_confirm, tv_wait_reply, tv_confirm_finish;
    private CustomViewPager viewpager;
    private View rl_progressbar;
    public static final int WARI_CONFIRM = 0;
    public static final int WARI_REPLY = 1;
    public static final int CONFIRM_FINISH = 2;
    public int currentIndex = WARI_CONFIRM;

    private ProduceFeedbackWaitConfirmFragment waitConfirmFragment;
    private ProduceFeedbackWaitReplyFragment waitReplyFragment;
    private ProduceFeedbackConfirmFinishFragment confirmFinishFragment;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    private ProductFeedbackListPresenter1 productFeedbackListPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_produce_feedback_fragment_ac);

        productFeedbackListPresenter = new ProductFeedbackListPresenter1(this);

        initView();

        tv_title.setText(getResources().getText(R.string.feedback_list));
        EventBus.getDefault().register(this);// 注册

        initData();
    }

    private void initView() {
        rl_back = findViewById(R.id.rl_back);
        rl_right=findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        viewpager = findViewById(R.id.viewpager);
        ll_wait_confirm = findViewById(R.id.ll_wait_confirm);
        ll_wait_reply = findViewById(R.id.ll_wait_reply);
        ll_confirm_finish = findViewById(R.id.ll_confirm_finish);
        tv_wait_confirm = findViewById(R.id.tv_wait_confirm);
        tv_wait_reply = findViewById(R.id.tv_wait_reply);
        tv_confirm_finish = findViewById(R.id.tv_confirm_finish);

        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        ll_wait_confirm.setOnClickListener(this);
        ll_wait_reply.setOnClickListener(this);
        ll_confirm_finish.setOnClickListener(this);

        waitConfirmFragment = new ProduceFeedbackWaitConfirmFragment();
        waitReplyFragment = new ProduceFeedbackWaitReplyFragment();
        confirmFinishFragment = new ProduceFeedbackConfirmFinishFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(waitConfirmFragment);
        listFragments.add(waitReplyFragment);
        listFragments.add(confirmFinishFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 等待确认
            case WARI_CONFIRM:
                ll_wait_confirm.setSelected(true);
                ll_wait_reply.setSelected(false);
                ll_confirm_finish.setSelected(false);

                tv_wait_confirm.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_wait_reply.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_confirm_finish.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 等待回复
            case WARI_REPLY:
                ll_wait_confirm.setSelected(false);
                ll_wait_reply.setSelected(true);
                ll_confirm_finish.setSelected(false);

                tv_wait_confirm.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_wait_reply.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_confirm_finish.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 已经确认
            case CONFIRM_FINISH:
                ll_wait_confirm.setSelected(false);
                ll_wait_reply.setSelected(false);
                ll_confirm_finish.setSelected(true);

                tv_wait_confirm.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_wait_reply.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_confirm_finish.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            // 返回
            case R.id.rl_back:
                finish();
                break;
            // 编辑反馈
            case R.id.rl_right:
                Intent mIntent = new Intent(ProduceFeedbackListFragmentActivity.this, ProduceEditFeedbackActivity.class);
                startActivity(mIntent);
                break;
            // 等待确认
            case R.id.ll_wait_confirm:
                if (currentIndex != WARI_CONFIRM) {
                    currentIndex = WARI_CONFIRM;
                    viewpager.setCurrentItem(WARI_CONFIRM);
                    showSelectedOption(WARI_CONFIRM);
                }
                break;
            // 等待回复
            case R.id.ll_wait_reply:
                if (currentIndex != WARI_REPLY) {
                    currentIndex = WARI_REPLY;
                    viewpager.setCurrentItem(WARI_REPLY);
                    showSelectedOption(WARI_REPLY);
                }
                break;
            //　已经确认
            case R.id.ll_confirm_finish:
                if (currentIndex != CONFIRM_FINISH) {
                    currentIndex = CONFIRM_FINISH;
                    viewpager.setCurrentItem(CONFIRM_FINISH);
                    showSelectedOption(CONFIRM_FINISH);
                }
                break;
        }
    }

    /**
     * 从服务器获取数据
     */
    private void initData() {
        productFeedbackListPresenter.productFeedbackList();
    }

    @Override
    public void showProductFeedbackListFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ProduceFeedbackListFragmentActivity.this, desc, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void getProductFeedbackListSuccessed(List<WorkPartFeedbackResponse> workPartFeedbackList, List<WorkPartFeedbackResponse> workPartFeedbackList_status_wait_confirm, List<WorkPartFeedbackResponse> workPartFeedbackList_status_wait_reply, List<WorkPartFeedbackResponse> workPartFeedbackList_status_confirm_finish, List<WorkPartFeedbackResponse> workPartFeedbackList_status_success) {
        if (waitConfirmFragment != null)
            waitConfirmFragment.initData(workPartFeedbackList_status_wait_confirm);

        if (waitReplyFragment != null)
            waitReplyFragment.initData(workPartFeedbackList_status_wait_reply);

        if (confirmFinishFragment != null)
            confirmFinishFragment.initData(workPartFeedbackList_status_confirm_finish);
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }

    public void onEventMainThread(ProduceForTeamEvent event) {
        if (null != productFeedbackListPresenter)
            productFeedbackListPresenter.productFeedbackList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解注册
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, null);
    }
}
